$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/userLevel/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 20, key: true },
			{ label: '会员等级', name: 'userLevel', index: 'user_level', width: 40 },
			{ label: '等级名称', name: 'levelName', index: 'level_name', width: 50 },
			{ label: '会员等级图标', name: 'levelImgUrl', index: 'level_img_url', width: 50,formatter: function (value, options, row) {
                    var title = "会员等级标识";
                    return "<img  style='width:30px;height: 30px;cursor:pointer;'  src='"+value+"'   onclick='showImg2(\""+value+"\",\""+title+"\")'/>";
                }},
			{ label: '最小消费', name: 'minConsume', index: 'min_consume', width: 50 },
			{ label: '最大消费', name: 'maxConsume', index: 'max_consume', width: 50 },
            { label: '公司提成比例', name: 'commissionRatio', index: 'commission_ratio', width: 50 },
            { label: '运营中心提成比例', name: 'operationCenterCommissionRatio', index: 'operation_center_commission_ratio', width: 30 },
			{ label: '充值开关', name: 'recharge', index: 'recharge', width: 50 ,formatter: function (value, options, row) {
			    if(value==1){
                    return "<span>开启</span>";
                }else{
                    return "<span>关闭</span>";
                }
			}},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
			{ label: '最后修改时间', name: 'updateTime', index: 'update_time', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });

});
var userLevelCode ;
var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        userLevel: {
		    recharge:1
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.userLevel = {};
            getAllEnumBy(null);
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
            var rowData=jQuery("#jqGrid").jqGrid("getRowData",id);
            userLevelCode = rowData.userLevel;
			vm.showList = false;
            vm.title = "修改";
            getAllEnumBy(userLevelCode);
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.userLevel.id == null ? "sys/userLevel/save" : "sys/userLevel/update";
                var levelStr = $("#userLevel option:selected").text();
                var levelName = levelStr.split("-")[0];
                var userLevel = levelStr.split("-")[1];
                vm.userLevel.userLevel = userLevel;
                vm.userLevel.levelName = levelName;
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.userLevel),
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/userLevel/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/userLevel/info/"+id, function(r){
                vm.userLevel = r.userLevel;
                // userLevelCode = r.userLevel.userLevel;
                $("#imageId").attr("src",r.userLevel.levelImgUrl);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:1
            }).trigger("reloadGrid");
		},
        fileUpload:function () {
            var formData = new FormData();
            formData.append('file', $('#file')[0].files[0]);
            $.ajax({
                url:baseURL + "sys/slideshow/fileUpload/",
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false
            }).done(function(res) {
                vm.userLevel.levelImgUrl = res.path;
                if(res.status=="true"){
                    $("#imageId").removeAttr("src");
                    $("#imageId").attr("src",res.path);
                }
            }).fail(function(res) {});
        }
	}
});

function getAllEnumBy(userLevelCode){
    $.ajax({
        type: "POST",
        url: baseURL + "sys/userLevel/getAllEnumBy",
        contentType: "application/json",
        success: function(r){
            if(r.code == 0){
                $("#userLevel").html(""); //绑定模号下拉菜单
                // $("#levelName").append($("<option value=\"\">全部</option>"));
                for (var i = 0; i < r.data.length; i++) {
                    if(null != userLevelCode && r.data[i].code == userLevelCode){
                        $("#userLevel").append($("<option value="+r.data[i].code+" selected='selected'>" +r.data[i].desc+"-"+r.data[i].code+ "</option>"));
                    }else{
                        $("#userLevel").append($("<option value="+r.data[i].code+">" +r.data[i].desc+"-"+r.data[i].code+ "</option>"));
                    }
                }
            }else{
                alert(r.msg);
            }
        }
    });
}
function showImg2(val,title){
    $("#showImg").attr("src",val);
    layer.open({
        type: 1,
        skin: 'layui-layer-molv',
        title: title,
        area: ['650px', '500px'],
        shade: 0,
        shadeClose: false,
        content: jQuery("#Img"),
        btn: ['确定'],
        btn1: function (index) {
            layer.close(index);
        }
    });
}